package com.lnking.lnkingplace.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.LeadersMapper;
import com.lnking.lnkingplace.model.dto.recordUser.RecordDetailDto;
import com.lnking.lnkingplace.model.dto.user.UserDto;
import com.lnking.lnkingplace.model.entity.Leaders;
import com.lnking.lnkingplace.model.entity.User;
import com.lnking.lnkingplace.model.request.leaders.LeadersAddRequest;
import com.lnking.lnkingplace.model.request.leaders.LeadersUpdateRequest;
import com.lnking.lnkingplace.service.ILeadersService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-09-03
 */
@Service
public class LeadersServiceImpl extends CustomServiceImpl<LeadersMapper, Leaders> implements ILeadersService {

    @Autowired
    UserUtil userUtil;

    public static final String  PWD ="123456";

    @Override
    public Leaders getLeaderByName(String username) {
        QueryWrapper<Leaders> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Leaders::getPhone, username);
        return getBaseMapper().selectOne(wrapper);
    }

    @Override
    public void update(LeadersUpdateRequest params) {
        Leaders leaders = getBaseMapper().selectById(params.getId());
        if(!leaders.getPhone().equals(params.getPhone())){
            // 防止重复
            QueryWrapper<Leaders> wapper = new QueryWrapper<>();
            wapper.lambda().eq(Leaders::getPhone, params.getPhone());
            Integer integer = getBaseMapper().selectCount(wapper);
            Assert.isTrue(integer == 0, "手机号码不能重复");
        }
        Leaders leader = new Leaders();
        BeanUtils.copyProperties(params, leader);
        leader.setPassword(StringUtils.isNotBlank(leader.getPassword()) ? DigestUtils.sha1Hex(leader.getPassword()) : null);
        updateById(leader);
    }

    @Override
    public Leaders getUserDetailById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public void add(LeadersAddRequest params) {
        // 防止重复
        QueryWrapper<Leaders> Swapper = new QueryWrapper<>();
        Swapper.lambda().eq(Leaders::getPhone, params.getPhone());
        Integer integer = getBaseMapper().selectCount(Swapper);
        Assert.isTrue(integer == 0, "手机号码不能重复");
        Leaders leaders = new Leaders();
        BeanUtils.copyProperties(params, leaders);
        // 获取上级的渠道
        UserDto user = userUtil.getUser();
        leaders.setChannel(user.getChannel());
        leaders.setCreatorTime(LocalDateTime.now());
        leaders.setPassword( DigestUtils.sha1Hex(PWD));
        getBaseMapper().insert(leaders);


    }
}
